/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.pig4cloud.pigx.contract.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.contract.entity.ContractOrderHistory;
import com.pig4cloud.pigx.contract.entity.ContractTrade;
import com.pig4cloud.pigx.contract.enums.ContractOrderEnum;
import com.pig4cloud.pigx.contract.mapper.ContractOrderHistoryMapper;
import com.pig4cloud.pigx.contract.mapper.ContractTradeMapper;
import com.pig4cloud.pigx.contract.mongo.entity.ContractSymbolMongo;
import com.pig4cloud.pigx.contract.service.ContractOrderHistoryService;
import com.pig4cloud.pigx.contract.utils.ContractUtils;
import com.pig4cloud.pigx.contract.vo.ContractOrderDetailedVO;
import com.pig4cloud.pigx.contract.vo.ContractTradeDetailedVO;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 历史订单表
 *
 * @since 2022-08-11 10:08:00
 */
@Service
@AllArgsConstructor
public class ContractOrderHistoryServiceImpl extends ServiceImpl<ContractOrderHistoryMapper, ContractOrderHistory> implements ContractOrderHistoryService {

    private final ContractUtils contractUtils;
    private final ContractTradeMapper contractTradeMapper;

    /**
     * app - 查询订单详情
     *
     * @param id 订单id
     * @return {@link ContractOrderDetailedVO}
     * @since 2022/9/21 17:05
     */
    @Override
    public ContractOrderDetailedVO appDetail(Long id) {
        // 查询订单详情
        ContractOrderHistory order = baseMapper.selectById(id);
        if (Objects.isNull(order)) {
            return null;
        }

        ContractOrderDetailedVO vo = new ContractOrderDetailedVO();
        BeanUtils.copyProperties(order, vo);
        // 翻译+拼接 类型
        String typeName = ContractOrderEnum.Type.findName(order.getType());
        String operationName = ContractOrderEnum.OperationType.findName(order.getOperationType());
        String sideName = ContractOrderEnum.Side.findName(order.getSide());
        vo.setType(typeName + "/" + operationName + sideName);
        // 翻译 状态
        String statusName = ContractOrderEnum.Status.findName(order.getStatus());
        vo.setStatus(statusName);
        // 收益率 * 100 给前端显示百分比
        if (vo.getProfitRate() != null) {
            vo.setProfitRate(vo.getProfitRate().multiply(BigDecimal.valueOf(100)));
        }
        // u本位数量
        vo.setCurrencyNum(vo.getNum().multiply(vo.getPrice()));

        // 查询币对详情
        ContractSymbolMongo symbol = contractUtils.getSymbolByName(vo.getSymbolName());
        // 主币外币名称
        vo.setMainCurName(symbol.getMainCurName());
        vo.setTokenCurName(symbol.getTokenCurName());
        if (vo.getDealNum() != null && vo.getDealAvg() != null) {
            vo.setCurrencyDealNum(vo.getDealNum().multiply(vo.getDealAvg()));
        }
        vo.setSheet(symbol.getSheet());
        // 设置精度
        contractUtils.setScaleBySymbol(vo, vo.getSymbolName());

        // 查询订单下成交记录列表
        QueryWrapper<ContractTrade> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(ContractTrade::getOrderId, id);
        List<ContractTrade> list = contractTradeMapper.selectList(wrapper);
        List<ContractTradeDetailedVO> tradeDetailedVOList = new ArrayList<>();
        list.forEach(e -> {
            ContractTradeDetailedVO detailedVO = new ContractTradeDetailedVO();
            BeanUtils.copyProperties(e, detailedVO);
            detailedVO.setCurrencyNum(detailedVO.getNum().multiply(detailedVO.getPrice()));
            tradeDetailedVOList.add(detailedVO);
            // 设置精度
            contractUtils.setScaleBySymbol(detailedVO, e.getSymbolName());
        });
        vo.setTradeList(tradeDetailedVOList);

        // 清空localThread币对缓存
        ContractUtils.removeSymbolThreadCache();
        return vo;
    }
}
